<?php
// 模块名_控制名
class Warehouse_ProductsBatchController extends Zend_Controller_Action {
	private $wpb; // 库存批次
	private $server;
	public function preDispatch() {
		$this->tplDirectory = "warehouse/productsbatch/"; // 控制对应模板目录
		$this->wpb = new Warehouse_Model_ProductsBatch ();
		$this->server = new Warehouse_Service_ProductsBatch ();
	}
	
	public function indexAction() {
	    $intpage=$this->_request->getParam('page',1);
	    $pageSize=$this->_request->getParam('pageSize','20');
	    
		$productsbatch = Warehouse_Service_ProductsBatch::getByCondition ( array (), '*', 'wpb_id', $pageSize, $intpage );
		$total =  Warehouse_Service_ProductsBatch::getByCondition ( array (), 'count(*)', 'wpb_id', $pageSize, $intpage );
		
		
// 		print_r($productsbatch);exit;
		$this->view->productsbatch = $productsbatch;
		$this->view->total = $total;
		
		echo Cff::renderTpl ( $this->tplDirectory . "index.tpl", "layout" );
	}
	
	public function listAction() {
		
		$ws_code = $this->_request->getParam ( 'ws_code', '' );
		$putaway_code = $this->_request->getParam ( 'putaway_code', '' );
		$receiving_code = $this->_request->getParam ( 'receiving_code', '' );
		$product_sku = $this->_request->getParam ( 'product_sku', '' );
		$customer_id = $this->_request->getParam ( 'customer_id', '' );
		
		$cond = array ();
		if (! empty ( $ws_code )) {
			$cond ['ws_code'] = trim ( $ws_code );
		}
		if (! empty ( $putaway_code )) {
			$cond ['putaway_code'] = trim ( $putaway_code );
		}
		if (! empty ( $receiving_code )) {
			$cond ['receiving_code'] = trim ( $receiving_code );
		}
		if (! empty ( $product_sku )) {
			$cond ['product_sku'] = trim ( $product_sku );
		}
		if (! empty ( $customer_id )) {
			$cond ['customer_id'] = trim ( $customer_id );
		}
		
		$cond ['wpb_status'] = 1;
		
		$page = $this->_request->getParam ( 'page', 1 );
		$pageSize = $this->_request->getParam ( 'pageSize' );
		$page = $page ? $page : 1;
		$orderby = 'wpb_last_update desc';
		$productsbatchArr = Warehouse_Service_ProductsBatch::getByCondition ( $cond, '*', $orderby, $pageSize, $page );
		$total = Warehouse_Service_ProductsBatch::getByCondition ( $cond, 'count(*)', '', 0, 0 );
		$result = array ();
		foreach ( $productsbatchArr as $key => $val ) {
			$tmpArr = array ();
			$tmpArr ['wpb_id'] = $val ['wpb_id'];
			$tmpArr ['ws_code'] = $val ['ws_code'];
			$tmpArr ['warehouse_id'] = $val ['warehouse_id'];
			$tmpArr ['putaway_code'] = $val ['putaway_code'];
			$tmpArr ['wpb_status'] = $val ['wpb_status'];
			$tmpArr ['receiving_code'] = $val ['receiving_code'];
			$tmpArr ['putaway_lot_number'] = $val ['putaway_lot_number'];
			$tmpArr ['product_id'] = $val ['product_id'];
			$tmpArr ['wpb_quantity'] = $val ['wpb_quantity'];
			$tmpArr ['wpb_quantity_hold'] = $val ['wpb_quantity_hold'];
			$tmpArr ['wpb_create_time'] = $val ['wpb_create_time'];
			$tmpArr ['wpb_last_update'] = $val ['wpb_last_update'];
			
			$rowsp = Product_Service_Product::getByProductId ( $val ['product_id'] );
			$tmpArr ['product_sku'] = $rowsp ['product_sku'];
			$tmpArr ['customer_id'] = $rowsp ['customer_id'];
			
			$result [] = $tmpArr;
		
		}
		
		$data = array ('ask' => 0 );
		if ($total > 0) {
			$data = array ('total' => $total, 'result' => $result, 'ask' => 1 );
		}
		die ( Zend_Json::encode ( $data ) );
	}
	
	/**
	 * 返回批次详细信息
	 */
	public function infoAction() {
		$_wpb_id = $this->_request->getParam ( 'wpb_id' );
		
		$return = array ('ask' => $_wpb_id, 'result' => null );
		if (! empty ( $_wpb_id )) {
			$rows = $this->wpb->getById ( $_wpb_id );
			$result = array ();
			if (! empty ( $rows )) {
				$result ['wpb_id'] = $rows ['wpb_id'];
				$result ['putaway_code'] = $rows ['putaway_code'];
				$result ['wpb_quantity'] = $rows ['wpb_quantity'];
				$result ['putaway_lot_number'] = $rows ['putaway_lot_number'];
				$result ['product_id'] = $rows ['product_id'];
			
			}
			
			$return = array ('ask' => 1, 'result' => $result );
		}
		die ( Zend_Json::encode ( $return ) );
	}
	
	/**
	 * 货位调整信息
	 */
	
	public function shelfinfoAction() {
		$_wpb_id = $this->_request->getParam ( 'wpb_id' );
		
		$return = array ('ask' => $_wpb_id, 'result' => null );
		if (! empty ( $_wpb_id )) {
			$rows = $this->wpb->getById ( $_wpb_id );
			$result = array ();
			if (! empty ( $rows )) {
				$result ['wpb_id'] = $rows ['wpb_id'];
				$result ['ws_code'] = $rows ['ws_code'];
				$result ['putaway_code'] = $rows ['putaway_code'];
				$result ['wpb_quantity'] = $rows ['wpb_quantity'];
				$result ['putaway_lot_number'] = $rows ['putaway_lot_number'];
				$result ['product_id'] = $rows ['product_id'];
			
			}
			
			$return = array ('ask' => 1, 'result' => $result );
		}
		die ( Zend_Json::encode ( $return ) );
	
	}
	
	public function editdataAction() {
        $option_data ['wpbId'] = $this->_request->getParam ( 'wpb_id' );
		$option_data ['intQuantity'] = $this->_request->getParam ( 'wpb_quantity' );
		$option_data ['strReason'] = $this->_request->getParam ( 'reason' );
	    $wspbpServer = new Warehouse_Service_ProductsBatchProccess($option_data);
		$result = $wspbpServer->editStocket();
		echo json_encode ( $result );
	}

	 /*
	  * 移库位
	  */
	public function editshlefAction() {
        $option_data ['wpbId'] = $this->_request->getParam ( 'wpb_id' );
		$option_data ['strWscode'] = $this->_request->getParam ( 'ws_code' );
        $option_data ['intQuantity'] = $this->_request->getParam ( 'move_quantity','0');
		$option_data ['strReason'] = $this->_request->getParam ( 'reason' );
        $wspbpServer = new Warehouse_Service_ProductsBatchProccess($option_data);
		$result = $wspbpServer->moveShelfStocket();
		echo json_encode ( $result );
	}
	
	public function editAction() {
		$wp_id = $this->_request->getParam ( 'wp_id', '' );
		$warehouse_id = $this->_request->getParam ( 'warehouse_id', '' );
		$ws_code = $this->_request->getParam ( 'ws_code', '' );
		$product_id = $this->_request->getParam ( 'product_id', '' );
		
		$wp_quantity = $this->_request->getParam ( 'wp_quantity', '' );
		$wh_primary = $this->_request->getParam ( 'wh_primary', '' );
		
		$row = array ('warehouse_id' => $warehouse_id, 'ws_code' => $ws_code, 'product_id' => $product_id, 

		'wp_quantity' => $wp_quantity, 'wh_primary' => $wh_primary );
		
		if ($wp_id) {
			$result = Warehouse_Service_Products::update ( $row, $wp_id );
		} else {
			$result = Warehouse_Service_Products::add ( $row );
		}
		
		if ($result) {
			$msg = array ('ask' => 1 );
		} else {
			$msg = array ('ask' => 0 );
		}
		echo json_encode ( $msg );
	}
	
	public function deleteAction() {
		$id = $this->_request->getParam ( 'id', '' );
		if (! empty ( $id )) {
			$result = Warehouse_Service_Products::delete ( $id );
			if ($result) {
				$msg = array ('ask' => 1 );
			} else {
				$msg = array ('ask' => 0 );
			}
			echo json_encode ( $msg );
		}
	
	}
	
	public function singleAction() {
		$wp_id = $this->_request->getParam ( 'id', '' );
		if ($wp_id) {
			$result = Warehouse_Service_Products::getByWpid ( $wp_id );
			$data = array ('ask' => 0 );
			if (count ( $result )) {
				$data = array ('result' => $result, 'ask' => 1 );
			}
			die ( Zend_Json::encode ( $data ) );
		}
	}
	/**
	 * 根据Product_id获取库存信息
	 */
	public function getinventoryAction() {
		$p_id = $this->_request->getParam ( 'product_id', '' );
		if ($p_id) {
			$result = Warehouse_Service_Products::getByProductId ( $p_id );
			$data = array ('ask' => 0 );
			if (count ( $result )) {
				$wrows = Warehouse_Service_Warehouse::getById ( $result ['warehouse_id'] );
				$result ['warehouse_id'] = $wrows ['warehouse_name'];
				$data = array ('result' => $result, 'ask' => 1 );
			}
			die ( Zend_Json::encode ( $data ) );
		}
	}
	
	public function getareaAction() {
		$warehouse_id = $this->_request->getParam ( 'id', '' );
		$result = Warehouse_Service_Area::getByWarehouseId ( $warehouse_id );
		$data = array ('ask' => 0 );
		if (count ( $result )) {
			$data = array ('result' => $result, 'ask' => 1 );
		}
		die ( Zend_Json::encode ( $data ) );
	}
	
	public function gethouseAction() {
		$wa_code = $this->_request->getParam ( 'id', '' );
		$result = Warehouse_Service_House::getByWaCode ( $wa_code );
		$data = array ('ask' => 0 );
		if (count ( $result )) {
			$data = array ('result' => $result, 'ask' => 1 );
		}
		die ( Zend_Json::encode ( $data ) );
	}

}